package com.fivestar.finance.app.ui.splash;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Handler;
import android.util.Log;

import com.fivestar.finance.app.R;
import com.fivestar.finance.app.ui.BaseActivity;
import com.fivestar.finance.app.ui.login.LoginActivity;

import java.io.IOException;

import javax.inject.Inject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

import static com.fivestar.finance.app.bean.req.FixOrderReq.JSON;

/**
 * 启动页面
 *
 * @author Derek.Kidd
 * @time 2016/4/14 16:14
 */
public class SplashActivity extends BaseActivity implements  ISplashView {


    @Inject
    SplashPresenter mPresenter;


    @Override
    protected int initContentView() {
        return R.layout.activity_start_up;
    }

    @Override
    public void initInjector() {
        DaggerSplashComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule())
                .splashModule(new SplashModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void initUiAndListener() {



      String json =  "{\"appId\":\"\",\"channelId\":\"\",\"inputCharset\":\"\",\"publicKeyIndex\":\"\",\"signAture\":\"\",\"data\":{\"custId\":\"258\",\"userId\":\"18221678256\",\"userType\":\"c\",\"prodId\":\"fenxi01\",\"merchId\":\"1107503056\",\"applyName\":\"王某某\",\"applyMobile\":\"18221678256\",\"applyAmt\":3000,\"applyTerm\":12,\"applyChannel\":\"pc\",\"applyDeviceId\":\"ddffvgff1452\",\"applySystemId\":\"fivestar-iou\"}}";



        //申明给服务端传递一个json串
        //创建一个OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个RequestBody(参数1：数据类型 参数2传递的json串)
        RequestBody requestBody = RequestBody.create(JSON, json);

        HttpLoggingInterceptor interceptor =   new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://221.226.187.91:8083/loan/productDefi")

                .post(requestBody)
                .build();

        //发送请求获取响应
        try {
            Response response=okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if(response.isSuccessful()){

                Log.i("================",response.body().string());

            }
        } catch (IOException e) {
            e.printStackTrace();
        }



        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent();
                intent.setClass(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }, 3000);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return false;
    }


    @Override
    protected void onResume() {
        super.onResume();

        //防止系统设置字体放大
        Resources resource = getResources();
        Configuration c = resource.getConfiguration();
        c.fontScale = 1.0f;
        resource.updateConfiguration(c, resource.getDisplayMetrics());
    }


}
